home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / conqsrc.lha / Conquest / src / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-20  |  6.5 KB  |  317 lines

  1. /* Init.c: Initializations routines. */
  2. #include <stdio.h>
  3. #include <sgtty.h>
  4. #include <stdlib.h>
  5.  
  6. #include "defs.h"
  7. #include "structs.h"
  8. #include "vars.h"
  9. #include "protos.h"
  10.  
  11. void initmach();
  12.  
  13. void setup_term()
  14. {
  15.   char *termname;
  16.   struct sgttyb ttyinfo;
  17.   
  18.   if ((termname = getenv("TERM")) != NULL) /* Some kind of TERM set */
  19.   {
  20.     printf("Terminal type is %s.\n", termname);
  21.     if (termname == "xterm") terminal_type = xterm;
  22.     if (termname == "vt100") terminal_type = vt100;
  23.     if (termname == "vt52") terminal_type = vt52;
  24.     if (termname == "adm3") terminal_type = adm3;
  25.     if (termname == "vis400") terminal_type = vis400;
  26.     if (termname == "hpterm") terminal_type = hpterm;
  27.     if (termname == "vi") terminal_type = vi; /* WHY??? */
  28.     if (termname == "concept") terminal_type = concept;
  29.     if (termname == "hardcopy") terminal_type = hardcopy;
  30.     if (terminal_type == unknown)
  31.     {
  32.       printf("Unknown terminal type %s - using vt100.\n", termname);
  33.       terminal_type = vt100;
  34.     }
  35.   }
  36.   else
  37.     terminal_type = vt100; /* For systems without $TERM */
  38.  
  39.   raw_fd = stdin;
  40.   ioctl(fileno(raw_fd), TIOCGETP, &ttyinfo);
  41.   ttyinfo.sg_flags |= RAW;
  42.   ioctl(fileno(raw_fd), TIOCSETP, &ttyinfo);
  43.   setbuf(raw_fd, NULL);
  44. }
  45.  
  46. void startup()
  47. {
  48.   printf("\n *** CONQUEST *** \n");
  49.   read_config(".conquestrc");
  50.   
  51.   setup_term();
  52.   
  53.   printf("\n* Welcome to CONQUEST! *\n\n");
  54.   printf("Amiga version 1.2.1\n");
  55.   printf("Hit return to continue\n");
  56.   get_char();
  57.   
  58.   printf("\33<");
  59.   
  60.   srand(time(0));
  61.  
  62.   initconst();
  63.   
  64.   init_player();
  65.  
  66.   initmach();
  67.  
  68.   /* Put player on board */
  69.   point(50,1);
  70.   print_star(tf[PLAYER][1].dest);
  71.   clear_field();
  72.  
  73.   /* And fight if in same place */
  74.   if (tf[ENEMY][1].dest == tf[PLAYER][1].dest)
  75.   {
  76.     clear_left();
  77.     battle();
  78.   }
  79. }
  80.  
  81. void assign_planets(tstar *ustar, int starnum)
  82. {
  83.   int i1, nplanets;
  84.   tplanet *pplanet;
  85.   
  86.   nplanets=rnd(4)-2;
  87.   if (nplanets < 0) nplanets = 1;
  88.   if (nplanets!=0)
  89.   {
  90.     if ((pplanet = (tplanet *)calloc(1, sizeof(tplanet))) == NULL)
  91.     {
  92.       printf("Out of memory. Sorry.\n");
  93.       exit(1);
  94.     }
  95.     ustar->first_planet=pplanet;
  96.     for (i1=1 ; i1<=nplanets; i1++)
  97.     {
  98.       pplanet->number = rnd(2) + (2*i1) - 2;
  99.       if (rnd(4)>2) pplanet->capacity = 10 * (rnd(4) + 2);
  100.       else pplanet->capacity = 5 * rnd(3);
  101.       pplanet->psee_capacity = pplanet->capacity;
  102.       pplanet->esee_def = 1;
  103.       pplanet->esee_team = NONE;
  104.       pplanet->pstar = starnum;
  105.       pplanet->team = NONE;
  106.       if (i1 < nplanets)
  107.       {
  108.     if ((pplanet->next = (tplanet *)calloc(1, sizeof(tplanet))) == NULL)
  109.     {
  110.       printf("Out of memory. Sorry.\n");
  111.       exit(1);
  112.     }
  113.     pplanet=pplanet->next;
  114.       }
  115.     }
  116.   }
  117. }
  118.  
  119. void initconst()
  120. {
  121.   int i1,x,y;
  122.  
  123.   /*init stars*/
  124.   for (i1 = 1; i1<=nstars; i1++) 
  125.   {
  126.     do 
  127.     {
  128.       x=rnd(bdsize);  
  129.       y=rnd(bdsize);
  130.     } 
  131.     while (board[x][y].star != '.');
  132.     stars[i1].x=x;     
  133.     stars[i1].y=y;
  134.  
  135.     board[x][y].star = 'A'+i1-1;
  136.     board[x][y].enemy = '?';
  137.     assign_planets(&stars[i1],i1);
  138.   }
  139.  
  140.   /*initialize task forces*/
  141.   tf[ENEMY][1].x = 1; 
  142.   tf[ENEMY][1].y = 1;
  143.  
  144.   for (i1 = ENEMY ; i1<=PLAYER; i1++ )
  145.     tf[i1][1].t = initunit;
  146.  
  147.   /* Small cheats for the enemy */
  148.   switch (rnd(3)) 
  149.   {
  150.    case 1: weapons[ENEMY] = rnd(4) + 2;
  151.     break;
  152.     
  153.    case 2: vel[ENEMY] = rnd(3);
  154.     break;
  155.     
  156.    case 3: growth_rate[ENEMY] = (float)(rnd(4) + 3) / 10.0;
  157.     break;
  158.   }
  159. }
  160.  
  161. void init_player()
  162. {
  163.   char str, key; 
  164.   int star_number;
  165.   int balance,cost,amt;
  166.   char iline[81];
  167.  
  168.   /* Startup */
  169.   printmap();
  170.   point(33,20);
  171.   printf("*Initialization*");
  172.  
  173.   do 
  174.   {
  175.     point(1,18);
  176.     printf("start at star?\n     ");
  177.     str = get_char();
  178.     point(1,19);
  179.     star_number= str-'A'+1;
  180.   } 
  181.   while ((star_number < 1) || (star_number > nstars));
  182.  
  183.   tf[PLAYER][1].x=stars[star_number].x;
  184.   tf[PLAYER][1].y=stars[star_number].y;
  185.   tf_stars[star_number][PLAYER]=1;
  186.   tf[PLAYER][1].dest = star_number;
  187.  
  188.   point(1,20);
  189.   printf("choose your initial fleet.");
  190.   point(1,21);
  191.   printf("you have %d transports", initunit);
  192.   point(1,22);
  193.   printf(" && %d units to spend", initmoney);
  194.   point(1,23);
  195.   printf("on ships or research.");
  196.  
  197.   print_res('V');
  198.   print_res('R');
  199.   print_res('W');
  200.  
  201.   balance = initmoney;
  202.   do
  203.   {
  204.     point(1,19);
  205.     print_tf(1);
  206.     point(1,18);
  207.     printf("%3d?                          ", balance);
  208.     point(6,18);
  209.     get_line(iline);
  210.     do
  211.     {
  212.       cost = 0;
  213.       key = get_token(iline,&amt);
  214.       switch (key)
  215.       {
  216.        case 'C':
  217.     cost = amt*c_cost;
  218.     if (cost < amt) break; /* Overflow */
  219.     if (cost <= balance)
  220.       tf[PLAYER][1].c=tf[PLAYER][1].c+amt;
  221.     break;
  222.        case 'S':
  223.     cost = amt*s_cost;
  224.     if (cost < amt) break; /* Overflow */
  225.     if (cost <= balance)
  226.       tf[PLAYER][1].s=tf[PLAYER][1].s+amt;
  227.     break;
  228.        case 'B':
  229.     cost = amt*b_cost;
  230.     if (cost < amt) break; /* Overflow */
  231.     if (cost <= balance)
  232.       tf[PLAYER][1].b=tf[PLAYER][1].b+amt;
  233.     break;
  234.        case 'H': 
  235.     help(0); 
  236.     break;
  237.        case 'W': 
  238.        case 'V': 
  239.        case 'R':
  240.     cost = amt;
  241.     if (cost <= balance)
  242.     {
  243.       research(PLAYER,key,amt);
  244.       print_res(key);
  245.     }
  246.     break;
  247.        case ' ': 
  248.     break;
  249.        case '>':
  250.     point(1,18);
  251.     printf(">?      ");
  252.     point(3,18);
  253.     key = get_char();
  254.     switch ( key )
  255.     {
  256.      case 'M': 
  257.       printmap(); 
  258.       break;
  259.      case 'R': 
  260.       ressum(); 
  261.       break;
  262.      default:
  263.       error(" !Only M,R during initialize");
  264.     } /*!= switch (*/
  265.     break;
  266.        default:
  267.     error( " !Illegal field %c",key);
  268.       } /*switch (*/
  269.       if (cost <= balance)
  270.     balance = balance - cost;
  271.       else
  272.       {
  273.     error("  !can't afford %d%c",amt, key);
  274.       }
  275.     } 
  276.     while (key != ' ');
  277.   } 
  278.   while (balance >0);
  279.   stars[star_number].visit[PLAYER]=true;
  280.   board[stars[star_number].x][stars[star_number].y].tf = 'a';
  281.   board[stars[star_number].x][stars[star_number].y].enemy=' ';
  282.   on_board(stars[star_number].x,stars[star_number].y);
  283.   point(33,20);
  284. }
  285.  
  286. void initmach()
  287. {
  288.   int res_amt, maxx, start_star, starnum, count;
  289.   float slist[MAX_NUM_STARS+1];
  290.   
  291.   en_research = 'V';
  292.   tf[ENEMY][1].c = 1;
  293.   tf[ENEMY][1].s = 2;
  294.   res_amt = 2;
  295.   research(ENEMY, en_research, res_amt);
  296.  
  297.   /* Found out the best place to start (most stars reachable) */
  298.   maxx = 0;
  299.   start_star = 0;
  300.   for (starnum = 1; starnum <= nstars; starnum++)
  301.   {
  302.     count = get_stars(starnum, slist);
  303.     count += rnd(5);
  304.     if (count > maxx)
  305.     {
  306.       maxx = count;
  307.       start_star = starnum;
  308.     }
  309.   }
  310.  
  311.   tf[ENEMY][1].dest = start_star;
  312.   tf[ENEMY][1].x = stars[start_star].x;
  313.   tf[ENEMY][1].y = stars[start_star].y;
  314.   stars[start_star].visit[ENEMY] = true;
  315.   tf_stars[start_star][ENEMY] = 1;
  316. }
  317.